#include "gtest/gtest.h"
#include "SingleLinkList.h"

// 测试构造函数和空链表
TEST(SingleLinkListTest, InitialState) {
    SingleLinkList list;
    EXPECT_TRUE(list.Empty());
    EXPECT_EQ(list.Size(), 0);
}

// 测试 PushFront 和 Get
TEST(SingleLinkListTest, PushFront) {
    SingleLinkList list;
    list.PushFront(10);
    list.PushFront(20);
    EXPECT_EQ(list.Size(), 2);
    EXPECT_EQ(list.Get(1), 20);
    EXPECT_EQ(list.Get(2), 10);
}

// 测试 PushBack
TEST(SingleLinkListTest, PushBack) {
    SingleLinkList list;
    list.PushBack(1);
    list.PushBack(2);
    list.PushBack(3);
    EXPECT_EQ(list.Get(1), 1);
    EXPECT_EQ(list.Get(3), 3);
    EXPECT_EQ(list.Size(), 3);
}

// 测试 Insert
TEST(SingleLinkListTest, Insert) {
    SingleLinkList list;
    list.PushBack(1);
    list.PushBack(3);
    list.Insert(2, 2); // 插入到中间
    EXPECT_EQ(list.Get(2), 2);
    EXPECT_EQ(list.Size(), 3);
}

// 测试 Remove 和 RemoveAll
TEST(SingleLinkListTest, RemoveAndRemoveAll) {
    SingleLinkList list;
    list.PushBack(1);
    list.PushBack(2);
    list.PushBack(3);
    list.Remove(2);
    EXPECT_FALSE(list.Find(2));
    list.PushBack(2);
    list.PushBack(2);
    list.RemoveAll(2);
    EXPECT_FALSE(list.Find(2));
}

// 测试 PopFront 和 PopBack
TEST(SingleLinkListTest, PopOperations) {
    SingleLinkList list;
    list.PushBack(1);
    list.PushBack(2);
    list.PushBack(3);
    list.PopFront();  // 删除1
    EXPECT_EQ(list.Get(1), 2);
    list.PopBack();   // 删除3
    EXPECT_EQ(list.Size(), 1);
    EXPECT_EQ(list.Get(1), 2);
}

// 测试 Set 和异常处理
TEST(SingleLinkListTest, SetAndGetOutOfRange) {
    SingleLinkList list;
    list.PushBack(5);
    list.Set(1, 100);
    EXPECT_EQ(list.Get(1), 100);

    EXPECT_THROW(list.Get(2), std::out_of_range);
    EXPECT_THROW(list.Set(0, 10), std::out_of_range);
}

// 测试 Clear
TEST(SingleLinkListTest, ClearList) {
    SingleLinkList list;
    list.PushBack(1);
    list.PushBack(2);
    list.Clear();
    EXPECT_TRUE(list.Empty());
    EXPECT_EQ(list.Size(), 0);
}

int main() {
    ::testing::InitGoogleTest();
    return RUN_ALL_TESTS();
}